from typing import List, Optional
from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session
from app.db.database import get_db
from app.db.models import User, JobDescription
from app.schemas import (
    JobDescription as JobDescriptionSchema,
    JobDescriptionCreate
)
from app.crud import (
    get_job_description,
    get_job_descriptions,
    create_job_description,
    delete_job_description
)
from app.core.security import get_current_active_user

# 创建职位描述路由器
router = APIRouter()

# 获取所有职位描述
@router.get("/", response_model=List[JobDescriptionSchema])
def read_job_descriptions(
    skip: int = 0,
    limit: int = 100,
    job_type: Optional[str] = None,
    industry: Optional[str] = None,
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_active_user)
):
    """
    获取职位描述列表，可以通过职位类型和行业进行筛选
    """
    job_descriptions = get_job_descriptions(db, skip=skip, limit=limit)
    
    # 根据筛选条件过滤结果
    if job_type:
        job_descriptions = [jd for jd in job_descriptions if jd.job_type == job_type]
    
    if industry:
        job_descriptions = [jd for jd in job_descriptions if jd.industry == industry]
    
    return job_descriptions

# 获取单个职位描述
@router.get("/{job_description_id}", response_model=JobDescriptionSchema)
def read_job_description(
    job_description_id: int,
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_active_user)
):
    """
    获取单个职位描述的详细信息
    """
    db_job_description = get_job_description(db, job_description_id=job_description_id)
    if db_job_description is None:
        raise HTTPException(status_code=404, detail="Job description not found")
    return db_job_description

# 创建新的职位描述
@router.post("/", response_model=JobDescriptionSchema, status_code=status.HTTP_201_CREATED)
def create_new_job_description(
    job_description: JobDescriptionCreate,
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_active_user)
):
    """
    创建新的职位描述
    """
    return create_job_description(db=db, job_description=job_description)

# 删除职位描述
@router.delete("/{job_description_id}", status_code=status.HTTP_204_NO_CONTENT)
def delete_existing_job_description(
    job_description_id: int,
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_active_user)
):
    """
    删除职位描述
    """
    db_job_description = get_job_description(db, job_description_id=job_description_id)
    if db_job_description is None:
        raise HTTPException(status_code=404, detail="Job description not found")
    
    delete_result = delete_job_description(db=db, job_description_id=job_description_id)
    if not delete_result:
        raise HTTPException(status_code=500, detail="Failed to delete job description")
    
    return None

# 根据职位类型获取职位描述
@router.get("/types/{job_type}", response_model=List[JobDescriptionSchema])
def read_job_descriptions_by_type(
    job_type: str,
    skip: int = 0,
    limit: int = 100,
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_active_user)
):
    """
    根据职位类型获取职位描述列表
    职位类型包括：互联网产品岗、金融风控岗、教育教学岗等
    """
    all_job_descriptions = get_job_descriptions(db, skip=skip, limit=limit)
    filtered_job_descriptions = [jd for jd in all_job_descriptions if jd.job_type == job_type]
    return filtered_job_descriptions

# 获取所有职位类型
@router.get("/types", response_model=List[str])
def read_all_job_types(
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_active_user)
):
    """
    获取所有职位类型列表
    """
    job_descriptions = get_job_descriptions(db)
    job_types = list(set([jd.job_type for jd in job_descriptions if jd.job_type]))
    return sorted(job_types)

# 根据行业获取职位描述
@router.get("/industries/{industry}", response_model=List[JobDescriptionSchema])
def read_job_descriptions_by_industry(
    industry: str,
    skip: int = 0,
    limit: int = 100,
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_active_user)
):
    """
    根据行业获取职位描述列表
    """
    all_job_descriptions = get_job_descriptions(db, skip=skip, limit=limit)
    filtered_job_descriptions = [jd for jd in all_job_descriptions if jd.industry == industry]
    return filtered_job_descriptions

# 获取所有行业
@router.get("/industries", response_model=List[str])
def read_all_industries(
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_active_user)
):
    """
    获取所有行业列表
    """
    job_descriptions = get_job_descriptions(db)
    industries = list(set([jd.industry for jd in job_descriptions if jd.industry]))
    return sorted(industries)